package com.gxa.adput.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.adput.mapper.AdPutMapper;
import com.gxa.adput.service.AdPutService;

import com.gxa.common.entity.platform_maintenance_service1001.AdPut;
import com.gxa.common.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdPutServiceImpl extends ServiceImpl<AdPutMapper, AdPut> implements AdPutService {

    @Autowired
    private AdPutMapper adPutMapper;
    @Override
    public R addAdPut(AdPut adPut) {
        R r = new R();

        int result = adPutMapper.insert(adPut);
        if(result > 0 ){
            r.setCode(200);
            r.setMessage("添加成功");
            r.setData(result);
        }else {
            r.setCode(500);
            r.setMessage("添加失败");
        }

        return r;
    }

    @Override
    public R updateAdPut(AdPut adPut) {
        R r = new R();
        int result = adPutMapper.updateAdPut(adPut);

        if(result > 0 ){
            r.setCode(200);
            r.setMessage("修改成功");
            r.setData(result);
        }else {
            r.setCode(500);
            r.setMessage("修改失败");
        }
        return r;
    }

    @Override
    public R selectAdPut(Integer currentPage, Integer pageSize) {
        R r = new R();
        QueryWrapper<AdPut> queryWrapper = new QueryWrapper<>();
        Page<AdPut> page = new Page<>(currentPage,pageSize);
        Page<AdPut> adPutPage = adPutMapper.selectPage(page, queryWrapper);
        r.setCode(200);
        r.setMessage("按照分页查询成功");
        r.setData(adPutPage);
        return r;
    }


    @Override
    public R delAdPutById(AdPut adPut) {
        R r = new R();
        long status = adPut.getStatus();
        if(status == 2){
            int result = adPutMapper.deleteById(adPut);
            if(result > 0){
                r.setCode(200);
                r.setMessage("删除成功");
                r.setData(result);
            }else {
                r.setCode(500);
                r.setMessage("删除失败");
            }
        }else if(status == 0){
            //根据id查询需要重新发布的内容
            AdPut selectAdPut = adPutMapper.selectById(adPut.getId());
            r.setCode(200);
            r.setMessage("重新发布成功");
            r.setData(selectAdPut);

        }else {
            r.setMessage("不需要删除");
        }
        return r;
    }

    @Override
    public R selectAdPutByCondition(Integer currentPage, Integer pageSize, AdPut adPut) {
        QueryWrapper<AdPut> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(adPut.getAdTitle() != null,"ad_title",adPut.getAdTitle())
                .eq(adPut.getClientSide() != null,"client_side",adPut.getClientSide())
                .eq(adPut.getLanguage() != null,"language",adPut.getLanguage())
                .eq(adPut.getStartTime() != null,"start_time",adPut.getStartTime())
                .eq(adPut.getCreationTime() != null,"creation_time",adPut.getCreationTime());
        Page<AdPut> page = new Page<>(currentPage,pageSize);
        Page<AdPut> adPutPage = adPutMapper.selectPage(page, queryWrapper);
        R r = new R();
        r.setCode(200);
        r.setMessage("按照条件查询成功");
        r.setData(adPutPage);
        return r;
    }


}
